package com.guyaning.deyson_ec.ec.main.index;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.guyaning.deyson_core.core.delegates.bottom.BottomItemDelegate;
import com.guyaning.deyson_core.core.ui.recycler.BaseDecoration;
import com.guyaning.deyson_core.core.ui.refresh.RefreshHandler;
import com.guyaning.deyson_ec.ec.R;
import com.guyaning.deyson_ec.ec.R2;
import com.guyaning.deyson_ec.ec.main.EcBottomDelegate;
import com.joanzapata.iconify.widget.IconTextView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/5.
 */

public class IndexDelegate extends BottomItemDelegate {

    @BindView(R2.id.srl_index)
    SwipeRefreshLayout mSwipeRefreshLayout = null;

    @BindView(R2.id.rl_index)
    RecyclerView mRecyclerView = null;

    @BindView(R2.id.icon_index_scan)
    IconTextView mIconIndexScan = null;

    @BindView(R2.id.et_search_view)
    AppCompatEditText mEtSearch = null;

    @BindView(R2.id.icon_index_message)
    IconTextView mIconIndexMessage = null;

    @BindView(R2.id.tb_index)
    Toolbar mTbIndex = null;

    private RefreshHandler mRefreshHandler = null;

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

        mRefreshHandler = RefreshHandler.create(mSwipeRefreshLayout,mRecyclerView,new IndexDataConverter());

    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_index;
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_dark,
                android.R.color.holo_orange_dark,
                android.R.color.holo_red_dark
        );

        mSwipeRefreshLayout.setProgressViewOffset(true, 60, 120);
    }

    private void initRecycleView(){

        final GridLayoutManager manager = new GridLayoutManager(getContext(),4);

        mRecyclerView.setLayoutManager(manager);

        mRecyclerView.addItemDecoration(BaseDecoration.create(ContextCompat.getColor(getContext(),R.color.app_backcolor),5));

        final EcBottomDelegate ecBottomDelegate = getParentDelegate();

        mRecyclerView.addOnItemTouchListener(IndexItemClickListener.create(ecBottomDelegate));

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        initRefreshLayout();

        initRecycleView();

        mRefreshHandler.firstPage("index.php",getContext());
    }

    @SuppressLint("InvalidR2Usage")
    @OnClick(R2.id.icon_index_scan)
    public void onViewClicked(View view) {
        ToastUtils.showShort("点击了扫描按钮");
    }
}
